﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.IO;

namespace EXAM.Admin.Page_Admin
{
    public partial class Reports : System.Web.UI.Page
    {
        string sErr = "";
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void dgvList_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            BUS.LIST_QDControl ctr = new BUS.LIST_QDControl();
            dgvList.DataSource = ctr.GetAll_LIST_QD("EXA", ref sErr);
        }

        protected void dgvList_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            if (e.CommandName.ToUpper() == "LOAD")
            {
                string id = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["QD_ID"].ToString();
                Response.Redirect("Filters.aspx?qdid=" + id);
            }
            else if (e.CommandName.ToUpper() == "DELETE")
            {
                string id = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["QD_ID"].ToString();
                BUS.LIST_QDControl qdCtr = new BUS.LIST_QDControl();
                sErr = qdCtr.Delete_LIST_QD("EXA", id);
                if (sErr == "")
                {
                    lbErr.Text = "Delete successfull";
                    dgvList.Rebind();
                }
            }
        }

        protected void btnTransferIn_Click(object sender, EventArgs e)
        {
            if (upFile.UploadedFiles.Count > 0)
            {
                string filename = MapPath(upFile.TargetFolder + "/" + Path.GetFileName(upFile.UploadedFiles[0].FileName));
                TransferInReport(filename);
            }
        }

        private void TransferInReport(string filename)
        {
            ExtractZipFile(filename);
        }

        private void ExtractZipFile(string filename)
        {
            MyZip zipPack = new MyZip();
            lbErr.Text = zipPack.Extract(filename, Path.GetDirectoryName(filename));
            string xmlFile = Path.GetDirectoryName(filename) + "/" + Path.GetFileNameWithoutExtension(filename) + ".xml";
            string xlsFile = Path.GetDirectoryName(filename) + "/" + Path.GetFileNameWithoutExtension(filename) + ".template.xls";
            if (File.Exists(xmlFile) && File.Exists(xlsFile))
            {
                sErr = TransferIn(xmlFile);
                if (sErr == "")
                {
                    try
                    {
                        File.Copy(xlsFile, MapPath("~/Template") + "/" + Path.GetFileNameWithoutExtension(filename) + ".template.xls", true);
                    }
                    catch (Exception ex) { sErr = ex.Message; }
                    if (sErr == "")
                        lbErr.Text = "Transfer In Successfull";
                }
            }
            else lbErr.Text = "Zip File Struct is not correct";
        }
        private string TransferIn(string filename)
        {
            string sErr = "";
            string _type = "QD";
            BUS.LIST_QDControl control = new BUS.LIST_QDControl();
            DataTable dt = control.ToTransferInStruct();
            foreach (DataColumn col in dt.Columns)
            {
                col.DataType = typeof(string);
            }
            BUS.CommonControl commonCtr = new BUS.CommonControl();
            try
            {
                dt.ReadXml(filename);
                dt = commonCtr.ValidatedDataTransferIn(dt, _type);

                try
                {
                    foreach (DataRow row in dt.Rows)
                    {
                        if ((bool)row["tmp_Validated"] == true)
                        {
                            if (_type == "QD")
                                control.TransferIn(row, ref sErr);
                        }
                    }
                    //Close();
                }
                catch (Exception ex) { sErr = ex.Message; }
            }
            catch (Exception ex) { sErr = ex.Message; }
            return sErr;
        }
    }
}
